﻿#region File Description
// EpicLeadManager
// EpicLeadManager
// GuildSQLManager.cs
#endregion

#region Using References
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows;
using EpicLeadManager.Models;

#endregion

namespace EpicLeadManager.Controllers.SQLManager
{
    public sealed class GuildSqlManager : SqLiteManager
    {
        #region Constructors
        public GuildSqlManager()
        {
            LoadDatabase<Guild>();

            if (Db == null)
                throw new DataException("sqlConnection");
        }
        #endregion

        #region CRUD
        /// <summary>
        ///     Adds the player.
        /// </summary>
        /// <param name="guildToAdd">The player to add.</param>
        public void AddGuild(Guild guildToAdd)
        {
            if (String.IsNullOrEmpty(guildToAdd.Name))
            {
                MessageBox.Show("Merci d'ajouter le nom de la guilde.");
            }
            else
            {
                int p = Db.Insert(guildToAdd);

                Console.WriteLine(@"New guild added in database : {0}", p);
            }
        }

        /// <summary>
        ///     Reads the player.
        /// </summary>
        /// <param name="guildName">Name of the player.</param>
        /// <returns></returns>
        public Guild ReadGuild(string guildName)
        {
            Guild guild = Db.Table<Guild>().FirstOrDefault(g => g.Name == guildName);

            return guild ?? new Guild();
        }

        /// <summary>
        ///     Reads all players.
        /// </summary>
        /// <returns></returns>
        public List<Guild> ReadAllGuilds()
        {
            return Db.Table<Guild>().ToList();
        }

        /// <summary>
        ///     Updates the player.
        /// </summary>
        /// <param name="guildToUpdate">The player to update.</param>
        public void UpdatePlayer(Guild guildToUpdate)
        {
            if (String.IsNullOrEmpty(guildToUpdate.Name))
            {
                MessageBox.Show("Merci d'ajouter le nom de la guilde.");
            }
            else
            {
                Db.Update(guildToUpdate);
            }
        }

        /// <summary>
        ///     Deletes the player.
        /// </summary>
        /// <param name="guildToDelete">The player to delete.</param>
        public void DeletePlayer(Guild guildToDelete)
        {
            Db.Delete(guildToDelete);
        }
        #endregion
    }
}